package admin

import (
	"object/go_cms/common"
	"object/go_cms/models"
)

type MenuAdminController struct {
	BaseAdminController
	models models.MenuModels
}

//菜单主页
// @router /admin/menu/indexUI [get]
func (c *MenuAdminController) IndexUI() {
	c.TplAdmin("menu/index")
}

//菜单添加页面
// @router /admin/menu/addUI [get]
func (c *MenuAdminController) AddMenuUI() {
	c.TplAdmin("menu/add")
}

//菜单修改页面
// @router /admin/menu/editUI [get]
func (c *MenuAdminController) EditMenuUI() {
	c.TplAdmin("menu/edit")
}

//获取菜单信息
// @router /admin/menu/get [get,post]
func (c *MenuAdminController) GetMenu() {
	id, err := c.GetInt64("id")
	if err != nil {
		c.Res.Code = 0
		c.Res.Msg = "参数有误"
	} else {
		c.Res.Code = 1
		c.Res.Msg = "获取成功"
		maps := make(map[string]interface{})
		maps["id"] = id
		c.Res.Data, err = c.models.GetMenu(maps)
		if err != nil {
			c.Res.Code = 0
			c.Res.Msg = "没有找到信息"
			c.Res.Data = nil
		}
	}
	c.Json(c.Res)
}

//获取菜单列表
// @router /admin/menu/query [get,post]
func (c *MenuAdminController) QueryMenu() {
	c.Res.Code = 0
	maps := common.GetParamMap(c.Input())
	c.Res.Data, _ = c.models.QueryMenu(maps)
	c.Json(c.Res)
}

//添加
// @router /admin/menu/add [post]
func (c *MenuAdminController) AddMenu() {
	maps := c.GetParamMap()
	c.Res.Code = 1
	c.Res.Msg = "添加成功"
	id, err := c.models.InsertMenu(maps)
	if err != nil {
		c.Res.Code = 0
		c.Res.Msg = "添加失败"
	}
	c.Res.Data = id
	c.Json(c.Res)
}

//修改
// @router /admin/menu/edit [post]
func (c *MenuAdminController) EditMenu() {
	maps := c.GetParamMap()
	c.Res.Code = 1
	c.Res.Msg = "修改成功"
	id, err := c.models.UpdateMenu(maps)
	if err != nil {
		c.Res.Code = 0
		c.Res.Msg = err.Error()
	}
	c.Res.Data = id
	c.Json(c.Res)
}

//删除
// @router /admin/menu/delete [post]
func (c *MenuAdminController) Delete() {
	c.Res.Code = 1
	c.Res.Msg = "删除成功"
	id, err := c.GetInt64("id")
	if err == nil {
		row, err := c.models.DeleteMenu(id)
		if err != nil {
			c.Res.Code = 0
			c.Res.Msg = err.Error()
		}
		c.Res.Data = row
	} else {
		c.Res.Code = 0
		c.Res.Msg = "参数错误"
	}
	c.Json(c.Res)
}
